package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*
* TwoLinesView
* */
class TwoLinesView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth0 = context.resources.getDimension(R.dimen.dp_2)
    private val mCorner = context.resources.getDimension(R.dimen.dp_10)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth0
            strokeCap = Paint.Cap.SQUARE  //线头
            strokeJoin = Paint.Join.ROUND //拐角
            style = Paint.Style.STROKE
            pathEffect = CornerPathEffect(mCorner)
            color = context.resources.getColor(R.color.color_FAE607)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth0
            strokeCap = Paint.Cap.SQUARE   //线头
            strokeJoin = Paint.Join.ROUND  //拐角
            style = Paint.Style.STROKE
            pathEffect = CornerPathEffect(mCorner)
            color = context.resources.getColor(R.color.color_F1896A)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    init {
        setLayerType(View.LAYER_TYPE_HARDWARE, null)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            mPath0.apply {
                moveTo(mPaint0.strokeWidth, measuredHeight / 2F)
                lineTo(mPaint0.strokeWidth, mPaint0.strokeWidth)
                lineTo(measuredWidth.toFloat() - mPaint0.strokeWidth, mPaint0.strokeWidth)
                lineTo(measuredWidth.toFloat() - mPaint0.strokeWidth, measuredHeight / 2F)
            }
            drawPath(mPath0, mPaint0)

            mPath1.apply {
                moveTo(measuredWidth.toFloat() - mPaint1.strokeWidth, measuredHeight / 2F)
                lineTo(measuredWidth.toFloat() - mPaint1.strokeWidth, measuredHeight.toFloat() - mPaint1.strokeWidth)
                lineTo(mPaint1.strokeWidth, measuredHeight.toFloat() - mPaint1.strokeWidth)
                lineTo(mPaint1.strokeWidth, measuredHeight / 2F)
            }
            drawPath(mPath1, mPaint1)
        }
    }
}